#!/bin/bash -e

# samtools 1.2 not for lower version

function info() {
echo Usage: `basename $0` '[-t thread(s)(2)] in.sam'
exit 65
}

while getopts  ":s:p:t:" opts
do
        case  $opts  in
        s) sample_name=$OPTARG;;
		p) out_prefix=$OPTARG;;
		t) threads=$OPTARG;;
		*) info;;
        esac
done
shift $(($OPTIND - 1))



if [ $# -lt 1 ]; then info; fi


. /mnt/ilustre/app/medical/tools/.var

if test -z $threads; then threads=2; fi

echo samtools sam to bam
samtools view -@$threads -bS $1 -o$out_prefix.bam && rm $1

echo samtools sort
samtools sort -@$threads -T$out_prefix.t -o$out_prefix.sort.bam $out_prefix.bam && rm $out_prefix.bam

echo samtools index
samtools index $out_prefix.sort.bam

. $cmd_done
